package cn.tangshh.universal.core.handler;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 可重复读取过滤器
 *
 * @author Tang
 * @version v1.0
 * @since 2024/05/31
 */
@Order(10)
@Component
public class RepeatableReadFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest && StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE))
            chain.doFilter(new RepeatableReadRequestWrapper((HttpServletRequest) request, response), response);
        else
            chain.doFilter(request, response);
    }
}
